<template>
  <div id="userRegister">
    <div>
      <img class="logo" src="../../../public/logo.png" />
    </div>
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
          v-model="userRegisterRequest.userAccount"
          label="账号"
          placeholder="请输入账号"
          size="large"
          :rules="[{ required: true, message: '请填写账号' }]"
        />
        <van-field
          v-model="userRegisterRequest.userName"
          label="用户名"
          placeholder="请输入用户名"
          size="large"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="userRegisterRequest.userPassword"
          type="password"
          label="密码"
          placeholder="请输入密码"
          size="large"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      <div style="margin: 16px">
        <van-button round block type="primary" native-type="submit"> 注册</van-button>
      </div>
    </van-form>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { ref } from 'vue'
import { userRegister } from '@/api/user'
import { showSuccessToast } from 'vant'

const router = useRouter()

const userRegisterRequest = ref({})

const onSubmit = async () => {
  const res = await userRegister(userRegisterRequest.value)
  if (res.code === 0) {
    showSuccessToast('注册成功')
  }
  await router.push('/user/login')
}
</script>

<style scoped>
.logo {
  height: auto;
  width: 100px;
  display: flex;
  margin: 130px auto 50px;
}
</style>
